package com.ssbc.security;

import com.ssbc.service.UserInfoGetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserInfoGetService userInfoGetService;
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/css/**", "/welcome").permitAll()
                    .antMatchers("/common/**").hasRole("user")
//                    .antMatchers("/vip/**").hasRole("VIP_USER")
                    .antMatchers("/admin/**").hasRole("admin")
                    //.antMatchers("/admin/**").permitAll()
                .and()
                .formLogin().loginPage("/login").failureUrl("/login_error").permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        System.out.println("用户密码加密:");
//        auth.userDetailsService(userInfoGetService).passwordEncoder(passwordEncoder());
        //测试用，直接在内存中写入user1用户
        /*
        auth
                .inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("user1")
                .password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
        System.out.println(new BCryptPasswordEncoder().encode("123456"));
        */
        //设定service作为用户信息获取的来源

    }
}
